# coding=utf-8
import datetime
import time

from models.judge import judge
import pymysql


def run_judge():
    db = pymysql.connect(host='bj-cynosdbmysql-grp-gc32r56a.sql.tencentcdb.com', user='root',
                         password='Genghe.114514', database='g_2', charset='utf8', port=25756)
    cursor = db.cursor()
    while True:
        sql = "select judge_id, code_language, codes, data_in_dic, data_out_dic, num, time_limit, memory_limit from OJTests where judged = 0"
        cursor.execute(sql)
        results = cursor.fetchone()
        if results is None:
            time.sleep(1)
            continue
        judge_id = results[0]
        lan = results[1]
        co = results[2]
        ans = ""
        for item in results[3]:
            if item != '\n':
                ans += item
            else:
                ans += "\\n"
        da_in = eval(ans)
        ans = ""
        for item in results[4]:
            if item != '\n':
                ans += item
            else:
                ans += "\\n"
        da_out = eval(ans)
        n = results[5]
        tl = results[6]
        ml = results[7]
        res = judge(language=lan, codes=co, data_in_d=da_in, data_out_d=da_out, num=n, time_limit=tl, memory_limit=ml)
        res = str(res).replace("'", '"')
        up_sql = "update OJTests set judge_result = '%s', judged = 1 where judge_id = '%d'" % (res, judge_id)
        lf = open("../work_dir/log.txt", mode="a")
        log = str(judge_id) + ' ' + str(datetime.datetime.now()) + ' ' + res + '\n'
        lf.write(log)
        lf.close()
        cursor.execute(up_sql)
        db.commit()


run_judge()

# print results
# print da_out
# print da_in
# res = judge(language=lan, codes=co, data_in_d=da_in, data_out_d=da_out, num=n, time_limit=tl, memory_limit=ml)
# print res
# res = "{'1': {'run_time': 0L, 'key': 'AC', 'memory_use': 192L}, '3': {'run_time': 0L, 'key': 'AC', 'memory_use': 192L}, '2': {'run_time': 0L, 'key': 'AC', 'memory_use': 196L}}"
# res = """{"1":"AC"}"""
# res = json.JSONEncoder(res)
# res = str(res).replace("'", '"')
# res = str(res).replace("'", '"')
# return_list = [{"judge_result": res, "judge_id": judge_id}]
# print return_list
# up_sql = "update test_judge set `judge_result`=%(judge_result)s and judged = 1 where judge_id=%(judge_id)s"
# up_sql = "update test_judge set judge_result = '%s', judged = 1 where judge_id = '%d'" % (res, judge_id)
# 执行SQL语句
# print up_sql
# cursor.executemany(up_sql, return_list)
# cursor.execute(up_sql)
# print judge_id
# 提交到数据库执行
# db.commit()
# except:
#     # 发生错误时回滚
#     db.rollback()
# while True:
# sql = "select * from test_judge where judged = 0"
# cursor.execute(sql)
# results = cursor.fetchone()
# judge_id = results[0]
# lan = results[1]
# co = results[2]
# da_in = eval(results[3])
# da_out = eval(results[4])
# n = results[5]
# tl = results[6]
# ml = results[7]
# print results
# # print da_out
# # print da_in
# res = judge(language=lan, codes=co, data_in_d=da_in, data_out_d=da_out, num=n, time_limit=tl, memory_limit=ml)
# print res
# # res = """{"1":"AC"}"""
# up_sql = "update test_judge set judge_result='%s' and judged='1' where judge_id='%d'" % (res, judge_id)
# try:
#     # 执行SQL语句
#     cursor.execute(up_sql)
#     print judge_id
#     # 提交到数据库执行
#     db.commit()
# except:
#     # 发生错误时回滚
#     db.rollback()

# def text():
#     data_inn = {"1": "12\n23", "2": "23\n34", "3": "13\n14"}
#     data_outt = {"1": "1223", "2": "2334", "3": "1314"}
#     # data_inn = {"1": "13 14"}
#     # data_outt = {"1": "27"}
#     # data_inn = {"1": "12 23", "2": "23 34"}
#     # data_outt = {"1": "35", "2": "57"}
#     an = judge(language="python3", codes="temp", data_in_d=data_inn, data_out_d=data_outt, num=3, time_limit=500,
#                memory_limit=65536)
#     print (an)
